/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ronsicecream.pos.stock.item;

import com.buanalintas.bynga.Action;
import com.buanalintas.bynga.Field;
import com.buanalintas.bynga.Page;
import com.ronsicecream.pos.entities.PosMasterItemStock;

/**
 *
 * @author user
 */
public class Item extends Page {

    private Field itemStockId;
    private Field itemStockValue;
    private Field itemStockTimestamp;
    private Field itemStockRegionalId;
    private Field itemStockPosUnitId;
//    private Field itemStockSatuanId;
    private Field itemStockItemId;
    private Action actionShow = new Action("show", "Show item stock");
    private Action actionExport = new Action("export", "Export item stock");
    private static String MESSAGE_UNIT = "<!--MESSAGE-UNIT-->";
    private static String MESSAGE_STATUS = "<!--MESSAGE-STATUS-->";
    private static String SELECT_UNIT_VALUES = "<!--SELECT-UNIT-VALUES-->";
    private static String SELECT_STATUS_VALUES = "<!--SELECT-STATUS-VALUES-->";
    private static String RECAP_EXPORT = "RECAP-EXPORT";
    private static String RECAP_EXPORT_LINK = "<!--RECAP-EXPORT-LINK-->";
    private static String RECAP_EXPORT_IMAGE = "<!--RECAP-EXPORT-IMAGE-->";
    private static String RECORD = "<!--RECORD-->";
    private static String LIST_DATA = "<!--LIST-DATA-->";
    private static String BUTTON_SEARCH = "<!--BUTTON-SEARCH-->";
    private static String MESSAGE_DAILY = "<!--MESSAGE-DAILY-->";
    private static String MESSAGE_WEEKLY = "<!--MESSAGE-WEEKLY-->";
    private static String MESSAGE_MONTHLY_YEAR = "<!--MESSAGE-MONTHLY-YEAR-->";
    private static String MESSAGE_MONTHLY_MONTH = "<!--MESSAGE-MONTHLY-MONTH-->";
    private static String MESSAGE_YEARLY = "<!--MESSAGE-YEARLY-->";
    private static String MESSAGE_CUSTOM = "<!--MESSAGE-CUSTOM-->";
    private static String SELECT_MODE = "<!--SELECT-MODE-->";
    private static String SELECT_MODE_ONCHANGE = "<!--SELECT-MODE-ONCHANGE-->";
    private static String SELECT_VALUES = "<!--SELECT-VALUES-->";
    private static String SELECT_STATUS = "<!--SELECT-STATUS-->";
    private static String SELECT_UNIT = "<!--SELECT-UNIT-->";
    private static String MODE_DAILY = "MODE-DAILY";
    private static String DAILY_SELECT_DATE = "<!--DAILY-SELECT-DATE-->";
    private static String DAILY_DATE_VALUE = "<!--DAILY-DATE-VALUE-->";
    private static String MODE_WEEKLY = "MODE-WEEKLY";
    private static String WEEKLY_START_DATE_VALUE = "<!--WEEKLY-START-DATE-VALUE-->";
    private static String WEEKLY_SELECT_START_DATE = "<!--WEEKLY-SELECT-START-DATE-->";
    private static String MODE_MONTHLY = "MODE-MONTHLY";
    private static String MONTHLY_SELECT_YEAR = "<!--MONTHLY-SELECT-YEAR-->";
    private static String MONTHLY_SELECT_YEAR_VALUES = "<!--MONTHLY-SELECT-YEAR-VALUES-->";
    private static String MONTHLY_SELECT_MONTH = "<!--MONTHLY-SELECT-MONTH-->";
    private static String MONTHLY_SELECT_MONTH_VALUES = "<!--MONTHLY-SELECT-MONTH-VALUES-->";
    private static String MODE_YEARLY = "MODE-YEARLY";
    private static String YEARLY_SELECT_YEAR = "<!--YEARLY-SELECT-YEAR-->";
    private static String YEARLY_SELECT_YEAR_VALUES = "<!--YEARLY-SELECT-YEAR-VALUES-->";
    private static String MODE_CUSTOM = "MODE-CUSTOM";
    private static String CUSTOM_SELECT_START_DATE = "<!--CUSTOM-SELECT-START-DATE-->";
    private static String CUSTOM_START_DATE_VALUE = "<!--CUSTOM-START-DATE-VALUE-->";
    private static String CUSTOM_SELECT_END_DATE = "<!--CUSTOM-SELECT-END-DATE-->";
    private static String CUSTOM_END_DATE_VALUE = "<!--CUSTOM-END-DATE-VALUE-->";
    private static String WARNING = "<!--WARNING-->";
    private static String TOTAL = "TOTAL";
    private static String TOTAL_CAPTION = "<!--TOTAL-CAPTION-->";
    private static String TOTAL_TEXT = "<!--TOTAL-TEXT-->";

    public Item() {
        super();

        setTheClass(PosMasterItemStock.class);




//        itemStockId = new Field();
//        itemStockId.setName("itemStockId");
//        itemStockId.setCaption(" stock ");
//        //itemStockId.setInfo("item stock id");
//        itemStockId.setListProperties(" align=\"left\" width=\"1%\" nowrap");
//        itemStockId.setInputProperties(" size=\"20\"");        
//        getFields().add(itemStockId);

        itemStockItemId = new Field();
        itemStockItemId.setName("itemStockItemId");
        itemStockItemId.setCaption("item");
        //itemSatuanId.setInfo("item satuan id");
        itemStockItemId.setListProperties(" align=\"left\" width=\"10%\" nowrap");
        itemStockItemId.setInputProperties(" size=\"20\"");
        getFields().add(itemStockItemId);


//        itemStockTimestamp = new Field();
//        itemStockTimestamp.setName("itemStockTimestamp");
//        itemStockTimestamp.setCaption("timestamp");
//        //itemTimestamp.setInfo("item timestamp");
//        itemStockTimestamp.setListProperties(" align=\"center\" width=\"5%\" nowrap");
//        itemStockTimestamp.setInputProperties(" size=\"12\"");
//        //itemTimestamp.setFormatter(DateFormatter.getInstanceDateTime(this));
//        getFields().add(itemStockTimestamp);

        itemStockRegionalId = new Field();
        itemStockRegionalId.setName("itemStockRegionalId");
        itemStockRegionalId.setCaption("regional ");
        //itemRegionalId.setInfo("item regional id");
        itemStockRegionalId.setListProperties(" align=\"left\" width=\"10%\" nowrap");
        itemStockRegionalId.setInputProperties(" size=\"20\"");
        getFields().add(itemStockRegionalId);

        itemStockPosUnitId = new Field();
        itemStockPosUnitId.setName("itemStockPosUnitId");
        itemStockPosUnitId.setCaption("Pos Unit");
        //itemStatusId.setInfo("item status id");
        itemStockPosUnitId.setListProperties(" align=\"left\" width=\"10%\" nowrap");
        itemStockPosUnitId.setInputProperties(" size=\"20\"");
        getFields().add(itemStockPosUnitId);

        itemStockValue = new Field();
        itemStockValue.setName("itemStockValue");
        itemStockValue.setCaption("value");
        //itemStockValue.setInfo("item stock value");
        itemStockValue.setListProperties(" align=\"left\" width=\"5%\" nowrap");
        itemStockValue.setInputProperties(" size=\"20\"");
        getFields().add(itemStockValue);

//        itemStockSatuanId = new Field();
//        itemStockSatuanId.setName("itemStockSatuanId");
//        itemStockSatuanId.setCaption("satuan");
//        //itemSatuanId.setInfo("item satuan id");
//        itemStockSatuanId.setListProperties(" align=\"left\" width=\"10%\" nowrap");
//        itemStockSatuanId.setInputProperties(" size=\"20\"");        
//        getFields().add(itemStockSatuanId);




        fillFields();

    }

    @Override
    public void beforeList() {
        if (!getCurrentUnit().getUnitId().equals(1)) {
            getController().setCondition("t.itemStockUnitId.unitId=" + getCurrentUnit().getUnitId());
        } else {
            return;
        }

    }
////    @Override
////    public StringBuffer process() {
////        if (getAction().equals(actionExport)) {
////            try {
////                updateExportParams();
////                if ("2".equals(getCurrentUnit().getUnitName())) {
////                    getResponse().setHeader("Content-Type", "application/vnd.ms-excel ");
////                } else {
////                    getResponse().setHeader("Content-Type", "application/pdf");
////                }
////                JRDataSource datasource = null;
////
////                String mode = getRequest().getParameter("mode");
////                String filterStatus = Utility.getParameterEmpty(getRequest(), "status");
////                String filterMore = Utility.getParameterEmpty(getRequest(), "more");
////                String filterLess = Utility.getParameterEmpty(getRequest(), "less");
////                EntityManager em = getController().getEntityManager();
////                PosMasterStatus status = null;
////                try {
////                    status = (PosMasterStatus) getEntityManager().createQuery("SELECT t FROM PosMasterStatus t WHERE t.statusId='" + Utility.getParameterEmpty(getRequest(), "filter") + "'").setHint(Persistence.REFRESH_HINT, true).getSingleResult();
////                    getExportParams().put("status", Utility.getParameterEmpty(getRequest(), "status") + " - " + status.getStatusId());
////                } catch (Exception e) {
////                    if ("*".equals(Utility.getParameterEmpty(getRequest(), "status"))) {
////                        getExportParams().put("status", Dictionary.getValue(this, "All Status"));
////                    } else {
////                        getExportParams().put("status", Utility.getParameterEmpty(getRequest(), "filter"));
////                    }
////                }
////
////                MasterUnit unit = null;
////                try {
////                    unit = (MasterUnit) getEntityManager().createQuery("SELECT t FROM MasterUnit t WHERE t.unitId='" + Utility.getParameterEmpty(getRequest(), "filter") + "'").setHint(Persistence.REFRESH_HINT, true).getSingleResult();
////                    getExportParams().put("unit", Utility.getParameterEmpty(getRequest(), "unit") + " - " + status.getStatusId());
////                } catch (Exception e) {
////                    if ("*".equals(Utility.getParameterEmpty(getRequest(), "unit"))) {
////                        getExportParams().put("unit", Dictionary.getValue(this, "All Unit"));
////                    } else {
////                        getExportParams().put("unit", Utility.getParameterEmpty(getRequest(), "unit"));
////                    }
////                }
////
////                getExportParams().put("title", Dictionary.getValue(this, "Item Stock Status") + " #" + Dictionary.getValue(this, "Status"));
////
////                getExportParams().put("filter", Dictionary.getValue(this, "Status") + " " + Utility.getPostParameterEmpty(this, "Status"));
////                if ("*".equals(Utility.getParameterEmpty(getRequest(), "status")) && "*".equals(Utility.getParameterEmpty(getRequest(), "unit"))) {
////                    getController().setWhere("t.itemStockValue >= " + filterMore + " AND t.itemStockValue <= " + filterLess + "");
////                } else if ("*".equals(Utility.getParameterEmpty(getRequest(), "status")) && !"*".equals(Utility.getParameterEmpty(getRequest(), "unit"))) {
////                    getController().setWhere("t.itemStockUnitId=" + unit + " t.itemStockValue >= " + filterMore + " AND t.itemStockValue <= " + filterLess);
////                } else if (!"*".equals(Utility.getParameterEmpty(getRequest(), "status")) && "*".equals(Utility.getParameterEmpty(getRequest(), "unit"))) {
////                    getController().setWhere("t.statusId=" + status + " t.itemStockValue >= " + filterMore + " AND t.itemStockValue <= " + filterLess + "");
////                } else {
////                    getController().setWhere("t.statusId=" + status + " AND t.itemStockUnitId=" + unit + " t.itemStockValue >= " + filterMore + " AND t.itemStockValue <= " + filterLess + "");
////                }
////                getController().setPaginationFilter("&mode=" + mode + "&status=" + filterStatus);
////
////
////                datasource = new JRBeanArrayDataSource(getController().getData(true).toArray());
////                JasperPrint jasperPrint = JasperFillManager.fillReport(getClass().getResourceAsStream("Stock.jasper"), getExportParams(), datasource);
////                if ("2".equals(getCurrentUnit().getUnitName())) {
////                    JRXlsExporter excelExporter = new JRXlsExporter();
////                    excelExporter.setParameter(JRXlsExporterParameter.JASPER_PRINT, jasperPrint);
////                    excelExporter.setParameter(JRXlsExporterParameter.OUTPUT_STREAM, getResponse().getOutputStream());
////                    excelExporter.exportReport();
////                } else {
////                    JasperExportManager.exportReportToPdfStream(jasperPrint, getResponse().getOutputStream());
////                }
////                return null;
////
////            } catch (Exception e) {
////                Logger.getInstance().log(e);
////            }
////            return new StringBuffer();
////
////        } else {
////            StringBuffer content = Utility.load(Bynga.BYNGA_PACKAGE_THEME + "content.html");
////            Template template = new Template();
////            template.loadTemplate(getClass().getResourceAsStream("/com/buanalintas/sandals/pos/masterstock/stock.html"));
////
////            template.addValue(MESSAGE_UNIT, Dictionary.getValue(this, "Please select unit"));
////            template.addValue(MESSAGE_STATUS, Dictionary.getValue(this, "Please select status"));
////
////            EntityManager em = getEntityManager();
////
////            StringBuffer selectMore = new StringBuffer();
////            String moreValue = Utility.getPostParameterEmpty(this, "more");
////            selectMore.append(moreValue);
////            template.addValue(SELECT_MORE_VALUES, "" + selectMore);
////            System.out.println("more :" + selectMore);
////
////            StringBuffer selectLess = new StringBuffer();
////            String lessValue = Utility.getPostParameterEmpty(this, "less");
////            selectLess.append(lessValue);
////            template.addValue(SELECT_LESS_VALUES, "" + selectLess);
////            System.out.println("less :" + selectLess);
////
////            StringBuffer selectUnit = new StringBuffer();
////            Query query = em.createQuery("SELECT t FROM MasterUnit t ORDER BY t.unitName");
////            String unitValue = Utility.getPostParameterEmpty(this, "unit");
////            for (Object object : query.getResultList()) {
////                MasterUnit unit = (MasterUnit) object;
////                selectUnit.append("<option value=\"" + unit.getUnitId() + "\"" + (unitValue.equals("" + unit.getUnitId()) ? " selected" : "") + ">" + unit.getUnitName() + "</option>");
////            }
////            template.addValue(SELECT_UNIT_VALUES, "" + selectUnit);
////
////            StringBuffer selectStatus = new StringBuffer();
////            String statusValue = Utility.getPostParameterEmpty(this, "status");
////            query = em.createQuery("SELECT t FROM PosMasterStatus t ORDER BY t.statusName");
////            for (Object object : query.getResultList()) {
////                PosMasterStatus status = (PosMasterStatus) object;
////                selectStatus.append("<option value=\"" + status.getStatusId() + "\"" + (statusValue.equals("" + status.getStatusId()) ? " selected" : "") + ">" + status.getStatusName() + "</option>");
////            }
////            template.addValue(SELECT_STATUS_VALUES, "" + selectStatus);
////
////            if (!"".equals(unitValue) && !"".equals(statusValue) && !"".equals(moreValue) && !"".equals(lessValue)) {
////                StringBuffer recapExportLink = template.getEmptyElement(RECAP_EXPORT);
////                Utility.replaceAll(recapExportLink, RECAP_EXPORT_LINK, "?m=" + getMenu().getMenuName() + "&a=" + actionExport.getName() + "&unit=" + unitValue + "&status=" + statusValue);
////                template.addElement(RECAP_EXPORT, recapExportLink);
////                getController().setCondition("t.itemStockUnitId=" + unitValue + " AND t.statusId=" + statusValue + " AND t.itemStockValue >= " + selectMore + " AND t.itemStockValue <= " + selectLess + "");
//////                template.addValue(RECORD, stockFormatter.format(getController().getCount()));
////                template.addValue(LIST_DATA, PageList.process(this).toString());
////            } else if (!"".equals(unitValue) && !"".equals(statusValue) && "".equals(moreValue) && "".equals(lessValue)) {
////                StringBuffer recapExportLink = template.getEmptyElement(RECAP_EXPORT);
////                Utility.replaceAll(recapExportLink, RECAP_EXPORT_LINK, "?m=" + getMenu().getMenuName() + "&a=" + actionExport.getName() + "&unit=" + unitValue + "&status=" + statusValue);
////                template.addElement(RECAP_EXPORT, recapExportLink);
////                getController().setCondition("t.itemStockUnitId=" + unitValue + " AND t.statusId=" + statusValue);
//////                template.addValue(RECORD, stockFormatter.format(getController().getCount()));
////                template.addValue(LIST_DATA, PageList.process(this).toString());
////            } else if (!"".equals(unitValue) && !"".equals(statusValue) && !"".equals(moreValue) && "".equals(lessValue)) {
////                StringBuffer recapExportLink = template.getEmptyElement(RECAP_EXPORT);
////                Utility.replaceAll(recapExportLink, RECAP_EXPORT_LINK, "?m=" + getMenu().getMenuName() + "&a=" + actionExport.getName() + "&unit=" + unitValue + "&status=" + statusValue);
////                template.addElement(RECAP_EXPORT, recapExportLink);
////                getController().setCondition("t.itemStockUnitId=" + unitValue + " AND t.statusId=" + statusValue + " AND t.itemStockValue >= " + selectMore);
//////                template.addValue(RECORD, stockFormatter.format(getController().getCount()));
////                template.addValue(LIST_DATA, PageList.process(this).toString());
////            } else if (!"".equals(unitValue) && !"".equals(statusValue) && "".equals(moreValue) && !"".equals(lessValue)) {
////                StringBuffer recapExportLink = template.getEmptyElement(RECAP_EXPORT);
////                Utility.replaceAll(recapExportLink, RECAP_EXPORT_LINK, "?m=" + getMenu().getMenuName() + "&a=" + actionExport.getName() + "&unit=" + unitValue + "&status=" + statusValue);
////                template.addElement(RECAP_EXPORT, recapExportLink);
////                getController().setCondition("t.itemStockUnitId=" + unitValue + " AND t.statusId=" + statusValue + " AND t.itemStockValue <= " + lessValue);
//////                template.addValue(RECORD, stockFormatter.format(getController().getCount()));
////                template.addValue(LIST_DATA, PageList.process(this).toString());
////            } else {
////                template.addElement(RECAP_EXPORT, null);
////            }
////
////            template.addValue(BUTTON_SEARCH, Dictionary.getValue(this, "Search"));
////            template.addValue(RECORD, null);
////
////            Utility.replaceAll(content, Template.CONTENT_CONTENT, template.process().toString());
////            updateContent(content, getMenu(), true);
////            return content;
////        }
////    }
}
